package com.aj.activity;

import java.io.File;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.aj.http.AsynchronizationHttpRequest.IHttpCallBack;
import com.aj.http.request.AlbumManagerRequest;
import com.aj.http.request.UploadPhotoRequest;
import com.aj.http.response.BaseRespond;
import com.aj.http.response.Common;
import com.aj.http.response.UploadPhotoResponse;
import com.aj.utils.Contants;

public class NewPhotoAlbumActivity extends BaseActivity {
    private EditText enter_albumname;
    private EditText enter_album_cover;
    private ImageView cover_image;
    private EditText enter_albumdescription;
    private String userid;
    private String filePath;
    private String type;
    private TextView text_albumdescription;
    private TextView text_album_cover;
    private TextView text_albumname;
    private TextView albumorzone_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_photoalbum);
        initView();
    }

    private void initView() {
        enter_albumname = (EditText) findViewById(R.id.enter_albumname);
        enter_album_cover = (EditText) findViewById(R.id.enter_album_cover);
        cover_image = (ImageView) findViewById(R.id.cover_image);
        enter_albumdescription = (EditText) findViewById(R.id.enter_albumdescription);
        text_albumdescription = (TextView) findViewById(R.id.text_albumdescription);
        text_album_cover = (TextView) findViewById(R.id.text_album_cover);
        text_albumname = (TextView) findViewById(R.id.text_albumname);
        albumorzone_title = (TextView) findViewById(R.id.albumorzone_title);
        if (getIntent().hasExtra(Contants.ALBUMORZONE_PARAMS)) {
            type = getIntent().getStringExtra(Contants.ALBUMORZONE_PARAMS);
        } else {
            type = "1";
        }
        if (type != null && type.trim().equals(Contants.ZONE_TYPE)) {
            setActionBarTitle(R.string.new_zone_text);
            albumorzone_title.setText(R.string.new_zone_text);
            text_albumname.setText(R.string.new_zone_photo_name_text);
            text_album_cover.setText(R.string.new_zone_photo_path_text);
            text_albumdescription.setText(R.string.new_zone_photo_description_text);
        } else {
            setActionBarTitle(R.string.new_album_text);
            text_albumname.setText(R.string.new_album_photo_name_text);
            text_album_cover.setText(R.string.new_album_photo_path_text);
            text_albumdescription.setText(R.string.new_album_photo_description_text);
            albumorzone_title.setText(R.string.new_album_text);
        }
        userid = getIntent().getStringExtra(Contants.USER_ID_PARAMS);
        ClickEvent clickEvent = new ClickEvent();
        setRightTextAndClickEvent1(getString(R.string.finish_text), clickEvent);
        cover_image.setOnClickListener(clickEvent);

    }

    class ClickEvent implements OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.cover_image:
                // Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, 2);
                break;
            case R.id.righttext_1:
                if (enter_albumname.getText().toString().trim().length() == 0) {
                    Toast.makeText(NewPhotoAlbumActivity.this, getString(R.string.newalbum_name_alert_text), Toast.LENGTH_LONG).show();
                    return;
                }
                if (enter_album_cover.getText().toString().trim().length() == 0) {
                    Toast.makeText(NewPhotoAlbumActivity.this, getString(R.string.newalbum_cover_alert_text), Toast.LENGTH_LONG).show();
                    return;
                }
                if (enter_albumdescription.getText().toString().trim().length() == 0) {
                    Toast.makeText(NewPhotoAlbumActivity.this, getString(R.string.newalbum_desc_alert_text), Toast.LENGTH_LONG).show();
                    return;
                }
                final File file = new File(filePath);
                showProgressDialog(getString(R.string.local_update_load_text));
                UploadPhotoRequest uploadPhotoRequest = new UploadPhotoRequest();
                uploadPhotoRequest.excute(NewPhotoAlbumActivity.this, null, null, "JPEG", file, new IHttpCallBack() {
                    @Override
                    public void success(String result) {
                        try {
                            UploadPhotoResponse updatePhotoResponse = new UploadPhotoResponse(new JSONObject(result));
                            Common common = updatePhotoResponse.getCommon();
                            if (common != null && common.getReturnCode().equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
                                new AlbumManagerRequest().excute(NewPhotoAlbumActivity.this, userid, "1", null, enter_albumname.getText().toString(),
                                        enter_albumdescription.getText().toString(), updatePhotoResponse.getPhotoUrl(), type, new IHttpCallBack() {
                                            @Override
                                            public void success(String result) {
                                                dimissProgressDialog();
                                                try {
                                                    Common common = new Common(new JSONObject(result));
                                                    if (common.getReturnCode().trim().equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
                                                        Intent intent = new Intent();
                                                        intent.setAction(Contants.REFRESH_PERSION_ALBUM_ACTION);
                                                        sendBroadcast(intent);
                                                        finish();
                                                    }
                                                    Toast.makeText(NewPhotoAlbumActivity.this, common.getErrorMsg(), Toast.LENGTH_LONG).show();

                                                } catch (JSONException e) {
                                                    e.printStackTrace();
                                                }
                                            }

                                            @Override
                                            public void fail(String result) {
                                                dimissProgressDialog();
                                            }
                                        });
                            } else {
                                dimissProgressDialog();
                                Toast.makeText(NewPhotoAlbumActivity.this, common.getErrorMsg(), Toast.LENGTH_LONG).show();
                            }
                        } catch (JSONException e) {
                            dimissProgressDialog();
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void fail(String result) {
                        dimissProgressDialog();
                        try {
                            UploadPhotoResponse updatePhotoResponse = new UploadPhotoResponse(new JSONObject(result));
                            Common common = updatePhotoResponse.getCommon();
                            Toast.makeText(NewPhotoAlbumActivity.this, common.getErrorMsg(), Toast.LENGTH_LONG).show();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                break;
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (RESULT_OK == resultCode) {
            if ((data != null) && (requestCode == 2)) {
                // Object object = data.getExtras().get("data");
                // ContentResolver resolver = getContentResolver();
                // pictureResult(data);
                Uri originalUri = data.getData(); // 获得图片的uri
                // bm = MediaStore.Images.Media.getBitmap(resolver,
                // originalUri); // 显得到bitmap图片
                String[] proj = { MediaStore.Images.Media.DATA };
                // 好像是android多媒体数据库的封装接口，具体的看Android文档
                Cursor cursor = getContentResolver().query(originalUri, proj, null, null, null);
                // 按我个人理解 这个是获得用户选择的图片的索引值
                int column_index = cursor.getColumnIndex(proj[0]);
                // 将光标移至开头 ，这个很重要，不小心很容易引起越界
                if (cursor.moveToFirst()) {
                    // 最后根据索引值获取图片路径
                    filePath = cursor.getString(column_index);
                    enter_album_cover.setText(filePath);
                    cursor.close();
                }
            }
        }
    }
}
